<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               v-model:search="search"
               v-model:page="page"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template #menu-left>
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.taskrecord_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>

      <template #status="{row}">
        <el-tag v-if="row.status === 0"  type="primary">{{row.$status}}</el-tag>
        <el-tag v-else-if="row.status === 1" type="warning">{{row.$status}}</el-tag>
        <el-tag v-else-if="row.status === 2"  type="success">{{row.$status}}</el-tag>
        <el-tag v-else type="danger">未完成</el-tag>
      </template>

      <template #score="{row}">
        <div  style="display: flex;align-items: center;">
            <avue-rate v-model="row.score" disabled></avue-rate>
            <el-link type="info" v-if="row.score>0">{{row.score}}</el-link>
        </div>
      </template>

    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/restaurant/taskrecord";
  import {mapGetters} from "vuex";
  import {baseUrl} from "@/config/env";

  export default {
    data() {
      return {
        form: {},
        query: {},
        search: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: '执行人',
              hide: true,
              editDisplay: false, // 编辑隐藏
              addDisplay: false, // 新增隐藏
              prop: 'userId',
              multiple: false,
              filterable: true,
              search: true,
              editDisabled: true, // 禁止编辑
              type: 'tree',
              dicUrl: baseUrl + '/restaurant/common/userAttendance',
              rules: [{
                required: true,
                message: '请输入执行人',
                trigger: 'click'
              }],
            },
            {
              label: '检查人',
              hide: true,
              editDisplay: false, // 编辑隐藏
              addDisplay: false, // 新增隐藏
              prop: 'inspectorId',
              multiple: false,
              filterable: true,
              search: true,
              editDisabled: true, // 禁止编辑
              type: 'tree',
              dicUrl: baseUrl + '/restaurant/common/userAttendance',
              rules: [{
                required: true,
                message: '请输入检查人',
                trigger: 'click'
              }],
            },
            {
              label: "任务日期",
              prop: "taskTime",
              rules: [{
                required: true,
                message: "请输入任务日期",
                trigger: "blur"
              }]
            },
            {
              label: "任务类型",
              prop: "timeType",
              type: "select",
              search: true,
              props: {
                label: 'dictValue',
                value: 'dictKey'
              },
              dicUrl: baseUrl + "/blade-system/dict/dictionary?code=task_time_type",
              rules: [{
                required: true,
                message: "请输入任务类型",
                trigger: "blur"
              }]
            },
            {
              label: "任务名称",
              prop: "taskName",
              search: true,
              rules: [{
                required: true,
                message: "请输入任务名称",
                trigger: "blur"
              }]
            },
            {
              label: "状态",
              prop: "status",
              type: "select",
              search: true,
              props: {
                label: 'dictValue',
                value: 'dictKey'
              },
              dicUrl: baseUrl + "/blade-system/dict/dictionary?code=task_record_status",
              rules: [{
                required: true,
                message: "请输入备注",
                trigger: "blur"
              }]
            },
            {
              label: "区域名称",
              prop: "areaName",
              rules: [{
                required: true,
                message: "请输入区域名称",
                trigger: "blur"
              }]
            },

            {
              label: "示例图",
              prop: "sampleGraph",
              type: "upload",
              overHidden: true,
              listType: "picture-card",
              action: "/api/blade-resource/oss/endpoint/put-file",
              propsHttp: {
                res: 'data',
                url: 'link'
              },
              rules: [{
                required: true,
                message: "请输入示例图",
                trigger: "blur"
              }]
            },
            {
              label: "注意事项",
              prop: "announcements",
              type: "textarea",
              overHidden: true,
              component: 'avue-ueditor',
              span: 24,
              minRows: 4,
            },
            {
              label: "执行人",
              prop: "userName",
              rules: [{
                required: true,
                message: "请输入执行人",
                trigger: "blur"
              }]
            },
            {
              label: "工作图片",
              prop: "userPicture",
              type: "upload",
              overHidden: true,
              listType: "picture-card",
              action: "/api/blade-resource/oss/endpoint/put-file",
              propsHttp: {
                res: 'data',
                url: 'link'
              },
              rules: [{
                required: true,
                message: "请输入工作图片",
                trigger: "blur"
              }]
            },
            {
              label: "工作建议",
              prop: "suggestion",
              type: "textarea",
              overHidden: true,
              rules: [{
                required: true,
                message: "请输入工作建议",
                trigger: "blur"
              }],
            },
            {
              label: "执行截止",
              prop: "deadline",
              rules: [{
                required: true,
                message: "请输入执行截止",
                trigger: "blur"
              }]
            },
            {
              label: "检查人",
              prop: "inspectorName",
              rules: [{
                required: true,
                message: "请输入检查人",
                trigger: "blur"
              }]
            },
            {
              label: "检查图",
              prop: "commentPicture",
              type: "upload",
              overHidden: true,
              listType: "picture-card",
              action: "/api/blade-resource/oss/endpoint/put-file",
              propsHttp: {
                res: 'data',
                url: 'link'
              },
              rules: [{
                required: true,
                message: "请输入检查图",
                trigger: "blur"
              }],
            },
            {
              label: "检查事项",
              prop: "comment",
              type: "textarea",
              overHidden: true,
              rules: [{
                required: true,
                message: "请输入检查事项",
                trigger: "blur"
              }]
            },
            {
              label: "检查截止",
              prop: "inspectDeadline",
              rules: [{
                required: true,
                message: "请输入检查截止",
                trigger: "blur"
              }]
            },
            {
              label: "评分",
              prop: "score",
              type: 'number',
              precision: 1,
              step: 0.5,
              min: 0,
              width: 160,
              rules: [{
                required: true,
                message: "请输入评分",
                trigger: "blur"
              }]
            },
            {
              label: "扣分原因",
              prop: "deductPoints",
              type: "textarea",
              overHidden: true,
              rules: [{
                required: true,
                message: "请输入扣分原因",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: false,
          viewBtn: this.validData(this.permission.taskrecord_view, false),
          delBtn: this.validData(this.permission.taskrecord_delete, false),
          editBtn: false
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
